<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Foreword;
use App\Models\Manage;
use App\Validate\ForewordValidate;
use Illuminate\Http\Request;

/**
 * 前言馆里
 */
class ForewordController extends CommonController
{

    public $forewordModelObj = null;
    public $validateObj = null;

    public function __construct(){
        parent::__construct();

        $this->validateObj = new ForewordValidate();
        $this->forewordModelObj = new Foreword();
    }

    /**
     * 前言列表
     * @param keyword 关键字搜索
     * @param page 页数    默认第一页
     * @param limit 限制条数   默认 10 条
     * @param start_time 开始时间
     * @param end_time 结束时间
     */
    public function forewordList(){
        $keyword = $this->request->input('keyword' , '');
        $start_time = $this->request->input('start_time' , '');
        $end_time = $this->request->input('end_time' , '');
        $limit = $this->request->input('limit' , '10');
        $page = $this->request->input('page' , 1);
        $res = $this->forewordModelObj->with('manageInfo')->select(['id','name','font_color','intro','subhead','img','img_thumb','template','roll_speed','page_stay_min_time','manage_id','create_time'])
        ->where(function($query) use($start_time , $end_time){
                if(!empty($start_time) && !empty($end_time)){
                    $query->whereBetween('create_time' , [$start_time , $end_time]);
                }
        })
        ->where(function($query) use($keyword){
            if($keyword){
                $query->orWhere('account' , 'like' , '%'.$keyword.'%')->orWhere('username' , 'like' , '%'.$keyword.'%');
            }
     })
        ->where('is_del' , 1)
        ->orderByDesc('id')
        ->paginate($limit)
        ->toArray();

        if($res['data']){
            $res = $this->disPageData($res);
            $res['data'] = $this->disDataSameLevel($res['data'], 'manage_info', [Manage::$manage_name=>'manage_name']);
            //增加序号
            $res['data'] = $this->addSerialNumber($res['data'], $page , $limit);
            
            return $this->returnApi(200 , '获取成功' , true , $res);
        }

        return $this->returnApi(203 , '暂无数据');
    }

    
    /**
     * 前言详情
     * @param id 作者id
     */
    public function forewordInfo()
    {
        //增加验证场景进行验证
        if (!$this->validateObj->scene('foreword_info')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validateObj->getError());
        }
        $res = $this->forewordModelObj->select(['id','name','intro','intro_en','subhead','font_color','img','img_thumb','template','roll_speed','page_stay_min_time','manage_id','create_time'])->where('is_del', 1)->find($this->request->id);
        if (empty($res)) {
            return $this->returnApi(202, '获取失败');
        }
        return $this->returnApi(200, '获取成功', true, $res);
    }

    /**
     * 前言添加
     * @param name 前言名称    必选
     * @param intro 前言简介
     * @param intro_en 前言简介英文
     * @param subhead 副标题
     * @param template 模板  1 图片作为内容显示  2  图片作为背景显示  
     * @param font_color 字体颜色
     * @param img 图片
     * @param roll_speed 前言文字滚动速率 
     * @param page_stay_min_time 页面停留的最短时间 单位秒
     */

     public function forewordAdd(){
        //增加验证场景进行验证
        if (!$this->validateObj->scene('foreword_add')->check($this->request->all())) {
            return $this->returnApi(201 ,  $this->validateObj->getError());
        }

        $is_exists = $this->forewordModelObj->forewordNameIsExists($this->request->name);
        if($is_exists){
            return $this->returnApi(202 , '此名称已存在，请重新添加'); 
        }

        $res = $this->forewordModelObj->add($this->request);


        if($res){
            return $this->returnApi(200 , '添加成功' , true);
        }

        return $this->returnApi(202 , '添加失败');
     }

     /**
     * 前言修改
     * @param id  前言id  必选
     * @param name 前言名称    必选
     * @param intro 前言简介
     * @param intro_en 前言简介英文
     * @param subhead 副标题
     * @param template 模板  1 图片作为内容显示  2  图片作为背景显示  
     * @param font_color 字体颜色
     * @param img 图片
     * @param roll_speed 前言文字滚动速率 
     * @param page_stay_min_time 页面停留的最短时间 单位秒
     */

    public function forewordChange(){
        //增加验证场景进行验证
        if (!$this->validateObj->scene('foreword_change')->check($this->request->all())) {
            return $this->returnApi(201 ,  $this->validateObj->getError());
        }
        $is_exists = $this->forewordModelObj->forewordNameIsExists($this->request->name ,$this->request->id);
        if($is_exists){
            return $this->returnApi(202 , '此名称已存在，请重新修改'); 
        }

        $res = $this->forewordModelObj->change($this->request);

        if($res){
            return $this->returnApi(200 , '修改成功' , true);
        }

        return $this->returnApi(202 , '修改失败');
     }

    /**
     * 前言删除
     * @param id  前言id  必选
     */

    public function forewordDel(){
        //增加验证场景进行验证
        if (!$this->validateObj->scene('foreword_del')->check($this->request->all())) {
            return $this->returnApi(201 ,  $this->validateObj->getError());
        }
        $res = $this->forewordModelObj->where('is_del' , 1)->find($this->request->id);
        if(empty($res)){
            return $this->returnApi(202 , '删除失败');
        }

        //判断展览是否有展览
        $is_exhibition = $this->forewordModelObj->foreIsExhibition($this->request->id);
        if ($is_exhibition) {
            return $this->returnApi(202, '此前言正在展览《' . $is_exhibition['name'] . '》中展览，请先取消后在进行删除');
        }

        $res->is_del = 2;
        $result = $res->save();

        if($result){
            return $this->returnApi(200 , '删除成功' , true);
        }

        return $this->returnApi(202 , '删除失败');
     }


}
